<?php

namespace App\Admin\Controllers;

use App\Admin\Handers\WeChatHander;
use App\Admin\Selectable\Scales;
use App\Admin\Selectable\Storey;
use App\Models\Room;
use App\Models\Scale;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;

class RoomController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '房间';

    protected $status = [   1 => '正常', 2 => '已预定', 3 => '使用中', 4 => '清扫中', 5 => '维修中'];

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Room());

        $grid->numbers('序号');
        $grid->rows(function ($row, $number) {
            $row->column('numbers', $number+1);
        });
        $grid->column('room_qrcode_img', '房间二维码')->myqrcode();
        $grid->column('store.name', __('门店'));
        $grid->column('number', __('房间号'));
        $grid->column('scale.name', __('房间类型'));
        $grid->column('Storey.name', __('楼层id'));
        $grid->column('describe', __('配置描述'));
        $grid->column('status', __('状态'))->using($this->status)
	        ->dot([1 => 'success', 2 => 'info', 3 => 'warning', 4 => 'primary', 5 => 'danger']);
        
        $grid->column('created_at', __('创建时间'))->sortable();

        $grid->filter(function($filter){
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            // 在这里添加字段过滤器
	        $filter->equal('scale.id', '房型名称')
		        ->multipleSelect(Scale::where('store_id', Admin::user()->store_id)->get()->pluck('name', 'id'));
            $filter->equal('number', '房间号');
            $filter->equal('status', '状态')->radio(Arr::add($this->status, '', '全部'));
        });


        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Room::findOrFail($id));

        $show->field('number', __('房号'));
        $show->field('scale.name', __('房型'));
        $show->field('Storey.name', __('楼层'));
        $show->field('describe', __('配置说明'));
        $show->field('status', __('状态'))
            ->using($this->status);
        $show->field('created_at', __('创建时间'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Room());

        $form->hidden('store_id', __('门店id'));
        
        if ($form->isCreating()){
            $form->text('number', __('房间号'))->rules(function ($form) {
                    // 如果不是编辑状态，则添加字段唯一验证
                    if (!$id = $form->model()->id) {
                        return 'required|unique:rooms,number,{{id}}';
                    }
            });

        }
        if ($form->isEditing()){
            $form->text('number',__('房间号'))
	            ->rules('required|unique:rooms,number,{{id}}');
            $form->radio('room_qrcode', '重新生成二维码')
	            ->options([1 => '是', 2 => '否'])
                ->default(2);
            $form->ignore(['room_qrcode']);
        }
            
        $form->select('scale_id', '房型')->options(
            Scale::where('store_id', Admin::user()->store_id)->get()->pluck('name', 'id')
        )->required();

        // 子表字段
        $form->belongsTo('storey_id', Storey::class, '楼层')->required();

        $form->text('describe', __('配置描述'))->required();
        $form->radio('status', __('状态'))
            ->options($this->status)->default(1);
        
        
        $form->saving(function (Form $form) {
            $form->store_id = Admin::user()->store_id;
            $room_qrcode = \request('room_qrcode');
            if ( $room_qrcode == 1) {
                $room_id = $form->model()->id;
		        $wechathaner = new WeChatHander();
		        $wechathaner->getStoreRoomQrCode(Admin::user()->store_id, $room_id);
            }
        });
        
        $form->saved(function (Form $form){
            // 没有二维码时创建二维码
            if(empty($form->model()->room_qrcode_img)){
	    		$room_id = $form->model()->id;
	    	    $wechathaner = new WeChatHander();
	    	    $wechathaner->getStoreRoomQrCode(Admin::user()->store_id, $room_id);
		    }
        });
        return $form;
    }
}
